package com.ruoyi.web;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.service.MyTokenService;
import com.ruoyi.system.domain.SeekerPdfRelation;
import com.ruoyi.system.domain.Upfile;
import com.ruoyi.system.service.ISeekerPdfRelationService;
import com.ruoyi.system.service.IUpfileService;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;


@RestController
public class MyUploadController {
    private static final Logger log = LoggerFactory.getLogger(MyUploadController.class);
    @Autowired
    private IUpfileService upfileService;
    @Autowired
    private ISeekerPdfRelationService iSeekerPdfRelationService;
    @Autowired
    private MyTokenService myTokenService;


    /**
     * 通用上传请求（单个）
     * @param uploadseekerid 上传的seekerid
     * @param file 上传的文件
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("uploadseekerid")String uploadseekerid) throws Exception {
        try {
            //上传文件的表单中input file那一个名字设置为name = "file"
            //<input type="file" name="file" id="file-uploader" accept=".jpg, .jpeg, .png, .pdf" >
           // System.out.println("完整文件名="+file.getOriginalFilename()+"\n"+file.getName()+"\n"+file.getContentType()+"\n"
            //+file.getSize()+"\n");

            Upfile upfile = new Upfile();
            upfile.setFileName(file.getOriginalFilename());
            upfile.setFileSize(file.getSize());
            upfile.setFileType(file.getContentType());
            byte[] fileBytes = file.getBytes(); // 将MultipartFile转换成字节数组
            //换成String

            upfile.setFile(java.util.Base64.getEncoder().encodeToString(fileBytes));

            upfileService.insertUpfile(upfile);
            Long upfileid = upfile.getFileId();
            //System.out.println("=========================================返回自增id"+upfileid);
            SeekerPdfRelation pdfRelation= new SeekerPdfRelation();
            pdfRelation.setPdfId(upfileid);
            pdfRelation.setSeekerId(Long.valueOf(uploadseekerid));
            //判断该seeker是否上传过简历
            if(iSeekerPdfRelationService.selectSeekerPdfRelationBySeekerId(Long.valueOf(uploadseekerid))!=null){
                iSeekerPdfRelationService.updateSeekerPdfRelation(pdfRelation);
            }
            else{
                //插入
                System.out.println("--------------------------插入表"+iSeekerPdfRelationService.selectSeekerPdfRelationBySeekerId(Long.valueOf(uploadseekerid)));
                iSeekerPdfRelationService.insertSeekerPdfRelation(pdfRelation);
            }
            //得到插入的
            Upfile getfile = upfileService.selectUpfileByFileId(upfileid);
            System.out.println("+++++++++++++++++++++++++++++++返回了getfile");
            //return getfile;

            // 上传文件路径
           // String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
           // String fileName = FileUploadUtils.upload(filePath, file);
           // String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("upfile", getfile);


            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }


}

